﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using ComicTool.Model;
using ImageTools.Controls;

namespace ComicTool
{
    public partial class MainPage : UserControl
    {
        private ComicView comic;

        public MainPage()
        {
            Utility.Dispatcher = Dispatcher;
            InitializeComponent();

            this.Drop += new DragEventHandler(MainPage_Drop);
            comic = new ComicView();
            DataContext = comic;

            if (!Application.Current.IsRunningOutOfBrowser)
            {
                string url;
                if (HtmlPage.Document.QueryString.TryGetValue("c", out url))
                {
                    comic.Load(url);
                }
            }
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            ShortcutKeyManager.Instance.AddListener(this);

            ShortcutKeyManager.Instance.AddShortcut(ModifierKeys.Control | ModifierKeys.Alt, Key.O, comic.OpenURLCommand);
            //manager.AddShortcut(ModifierKeys.Control | ModifierKeys.Alt, Key.F, comic.OpenFileCommand);
            ShortcutKeyManager.Instance.AddShortcut(ModifierKeys.Control | ModifierKeys.Alt, Key.E, comic.EditComicCommand);

            ShortcutKeyManager.Instance.AddShortcut(ModifierKeys.None, Key.Space, comic.NextCommand);
            ShortcutKeyManager.Instance.AddShortcut(ModifierKeys.Control, Key.Right, comic.NextCommand);
            ShortcutKeyManager.Instance.AddShortcut(ModifierKeys.Control, Key.Left, comic.PreviousCommand);

            ShortcutKeyManager.Instance.AddShortcut(ModifierKeys.Control, Key.Up, () => Button_Click_4(null, null));
            ShortcutKeyManager.Instance.AddShortcut(ModifierKeys.Control, Key.Down, () => Button_Click_5(null, null));
            ShortcutKeyManager.Instance.AddShortcut(ModifierKeys.Control | ModifierKeys.Alt, Key.A, () => Button_Click_3(null, null));

            DispatcherTimer timer = new DispatcherTimer();
            timer.Tick += (s, a) =>
                {
                    VisualStateManager.GoToState(this, "Hide", true);
                    timer.Stop();
                };
            timer.Interval = new TimeSpan(0, 0, 3);
            timer.Start();
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            Container.ScalingMode = ImageEditorScalingMode.Auto;
        }

        //can't figure why this can't be bound properly
        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            Container.ScalingMode = ImageEditorScalingMode.FixedScaling;
            Container.Scaling = Math.Max(0.1, Math.Round(Container.Scaling + 0.1, 2));
        }

        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            Container.ScalingMode = ImageEditorScalingMode.FixedScaling;
            Container.Scaling = Math.Max(0.1, Math.Round(Container.Scaling - 0.1, 2));
        }

        private void MainPage_Drop(object sender, DragEventArgs e)
        {
            if (e.Data != null)
            {
                FileInfo[] files = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];
                if (files.Length > 0)
                {
                    comic.Load(files[0]);
                }
            }
        }

        private void grid_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            VisualStateManager.GoToState(this, "Show", true);
        }

        private void grid_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            VisualStateManager.GoToState(this, "Hide", true);
        }

        private void Button_Click_7(object sender, RoutedEventArgs e)
        {
            About a = new About();
            a.Show();
        }

        private void Grid_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (comic.NextCommand.CanExecute(null))
            {
                comic.NextCommand.Execute(null);
            }
        }
    }
}
